import { createRouter,createWebHashHistory } from "vue-router";
import { getQx } from '@/api/index';
import store from '@/store';
const routes=[
    {
        path:'/',
        name:'Login',
        component:() => import('@/views/login.vue'),
        meta:{
            title:'登录',
            auth:[],
            icon:''
        }
    },    
    {
        path:'/Center',
        name:'Index',
        component:() => import('@/views/index.vue'),
        redirect:'/dashBoard',
        meta:{
            title:'首页',
            auth:[],
            icon:''
        },
        children:[
            {
                path:'/dashBoard',
                name:'dashBoard',
                component:() => import('../views/setting/dashBoard.vue'),
                meta:{
                    title:'控制台',
                    auth:[],
                    icon:''
                }
            }
        ]
    },
    {
        path:'/404',
        name:'Error',
        component:() => import('@/views/S404.vue'),
        meta:{
            title:'错误',
            auth:[],
            icon:''
        }
    },
    

];


const router=createRouter({
    history:createWebHashHistory(),
    routes
});

router.beforeEach((to,from,next) =>{
    let token=store.getters['auth/tokken'];
    if(to.path=='/'){
        next();
    }
    else if(store.state.auth.menulist.length)
    {
        next();
    }
    else if(token)
    {
        getQx({token:token}).then(
            res=>{
                if(res.data['code']==200)
                {
                    store.commit('auth/loadroutes',res.data['data']['biaoshi']);  
                    if(to.path!='/Center')
                    {                        
                        //console.log('****');
                        let arr=to.path.split('/');
                        next('/Center');                        
                    }

                   
                          
                                            
                }
                else{
                    console.log(res);
                    next('/');//这里是过期
                }
            },
            err=>{
                console.log(err);
                next('/');//错误
            }
        );
    }
    else{

        next('/');
    }
});


export default router;
export { asyncroutes } from './asyncroutes';
